
uniform mat4 matModelViewProj;
uniform mat4 matModelView;
uniform mat4 matModelViewInverseTranspose;
uniform vec3 vecEyePositionWorld;
uniform vec3 vecLightPositionWorld;

attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord;
attribute vec4 a_tangent;

varying vec3 vViewVec;
varying vec3 vLightVec;
varying vec2 vTexCoord;

void main()
{
	vec4 position =  matModelView * a_position;
	vec3 normal = normalize((matModelViewInverseTranspose * vec4(a_normal, 0.0)).xyz);
	vec3 tangent = normalize((matModelViewInverseTranspose * a_tangent).xyz);
	vec3 bitangent =  normalize(a_tangent.w*cross(normal,tangent));

	// Compute world-space direction vector
	vec3 viewDirectionWorld = vecEyePositionWorld - position.xyz;
	// Compute world-space light direction vector
	vec3 lightDirectionWorld = vecLightPositionWorld - position.xyz;

	// Create the tangent matrix
	mat3 tangentMat = mat3( tangent, bitangent, normal );
	// Transform the view and light vectors into tangent space
	vViewVec = viewDirectionWorld * tangentMat;
	vLightVec = lightDirectionWorld * tangentMat;

	// set position and pass texture coordinate to fragment shader
	gl_Position =  matModelViewProj * a_position;
	vTexCoord    = a_texCoord;
}